/**
 * SrvUtilityPortlets -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of SrvUtilityPortlets.
 * 
 * SrvUtilityPortlets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SrvUtilityPortlets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SrvUtilityPortlets.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEA.ALI.Web.UI;
using com.plumtree.server;
using AluiServerLib.Helper;

namespace ALUIServerUtilities
{
    public partial class ObjectIdentifyer : ALIPortletPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                IPTSession session = NativeApplicationMgr.GetInstance().GetNativeSession(m_portletRequest.GetLoginToken());

                //get the migration manager to deal with uuids
                IPTMigrationManager migrationMrg = (IPTMigrationManager)session.OpenGlobalObject(PT_GLOBALOBJECTS.PT_GLOBAL_MIGRATION_MANAGER, false);

                int objectID = 0;
                int classID = 0;

                string ptobjectuuid = txtObjectUUID.Text;
                if (!"".Equals(ptobjectuuid))
                {
                    try
                    {
                        Object[] tempids = migrationMrg.UUIDToObjectID(ptobjectuuid);
                        classID = (int)tempids[PT_MIGRATION_OBJECT_COLS.PT_MOC_CLASSID];
                        objectID = (int)tempids[PT_MIGRATION_OBJECT_COLS.PT_MOC_OBJECTID];
                    }
                    catch (Exception exc)
                    {
                        Response.Write("<br><font color=\"red\">The UUID provided [" + ptobjectuuid + "] is not valid or does not correspond to a valid object.</font>");
                        return;
                    }
                }
                else
                {
                    try
                    {
                        objectID = int.Parse(txtOjectID.Text);
                    }
                    catch (Exception exc)
                    {
                        objectID = 0;
                    }

                    try
                    {
                        classID = int.Parse(txtClassID.Text);
                    }
                    catch (Exception exc)
                    {
                        classID = 0;
                    }
                }

                if (objectID > 0 && classID > 0)
                {
                    IPTObjectManager objectMgr = session.GetObjectManagers(classID);
                    IPTObject ptobject = null;
                    try
                    {
                        ptobject = (IPTObject)objectMgr.Open(objectID, false);
                    }
                    catch (Exception exc)
                    {
                        ptobject = null;
                    }
                    if (null != ptobject)
                    {
                        Response.Write("<br><br>Object ID / Class ID:");
                        Response.Write("<br>Object ID: " + ptobject.GetObjectID());
                        Response.Write("<br>Class ID: " + ptobject.GetClassID());

                        //display where this object's name and location
                        Response.Write("<br><br>Object Name and Description:");
                        if (ptobject.GetSupportsLocalization())
                        {
                            Response.Write("<br>" + ptobject.GetLocalizedName());
                            Response.Write("<br>" + ptobject.GetLocalizedDescription());
                        }
                        else
                        {
                            Response.Write("<br>" + ptobject.GetName());
                            Response.Write("<br>" + ptobject.GetDescription());
                        }
                        Response.Write("<br><br>Object UUID:");
                        Response.Write("<br>" + migrationMrg.ObjectIDToUUID(classID, objectID));

                        Response.Write("<br><br>Object Path:");
                        int folderId = ptobject.GetAdminFolderID();
                        if (folderId > 0)
                        {
                            IPTAdminFolder ptadminfolder = null;
                            IPTAdminCatalog m_ptCatalog = (IPTAdminCatalog)session.GetAdminCatalog();
                            try
                            {
                                ptadminfolder = m_ptCatalog.OpenAdminFolder(folderId, false);
                            }
                            catch (Exception exc)
                            {
                                ptadminfolder = null;
                            }

                            if (null != ptadminfolder)
                            {
                                Response.Write("<br>" + ptadminfolder.GetPath());
                            }
                        }
                    }
                    else
                    {
                        Response.Write("<br><font color=\"red\">ObjectID " + objectID + " (class:" + classID + ") Not Found!!</font>");
                    }
                }
                else
                {
                    Response.Write("<br><font color=\"red\">ObjectID " + objectID + " (class:" + classID + ") are not formatted right. They both must be valid numbers.</font>");
                }
                pnlObjectIdentifyer.Visible = false;
                Response.Write("<br><br><a href=\"pt://return\">Return To Portal</a>");
            }
        }
    }
}
